<template>
  <div class="auxil_agent_box">
    <el-tabs
      class="auxil_agent_tabs"
      :active-name="activeName"
      @tab-click="tabChange"
    >
      <!-- v-if="webchatData && webchatData.robotAssistId" -->
      <el-tab-pane
        v-if="isHaveRobot"
        :label="$t('webchat.AIRobot')"
        name="AIRobot"
      >
        <robot-search ref="robotSearch" :showrobotselect="true"></robot-search>
      </el-tab-pane>
      <el-tab-pane :label="$t('routeI18n.km')" name="km">
        <km-search :handle="['preview', 'copy']" ref="kmSearch"></km-search>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script type="text/javascript">
import bus from '@/store/eventBus'
import kmSearch from '@/components/public-modules/km-search/index.vue'
import robotSearch from '@/components/public-modules/robot-search/index.vue'
export default {
  name: 'webchatAuxilAgent',
  props: {
    showrobotsearch: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      activeName: 'AIRobot',
      isHaveRobot: false
    }
  },
  computed: {
    sessionId () {
      let arr = this.$route.path.split('/')
      return arr[4]
    },
    tabType () {
      let arr = this.$route.path.split('/')
      return arr[3]
    },
    webchatData () {
      //  会话的详细信息
      if (this.tabType === 'webchat_todo' && this.$store.state.webchat.current.webchat_todo.currentSession[this.sessionId]) {
        return this.$store.state.webchat.current.webchat_todo.currentSession[this.sessionId]
      } else {
        return {}
      }
    }
  },
  methods: {
    tabChange (tab, event) {
      if (tab.name === 'km') {
        // tab切换到知识库
        if (this.$refs.robotSearch) {
          let search = this.$refs.robotSearch.kmSearchContent
          this.activeName = 'km'
          if (search) {
            // bus.$emit('fastSearchKm', { data: search })
            this.$refs.kmSearch.setKmSearchContent(search)
          } else {
            this.$refs.kmSearch.searchInnerKmList()
          }
        }
      } else {
        // tab切换到智能回答
        let search = this.$refs.kmSearch.kmSearchContent
        this.activeName = 'AIRobot'
        console.log(search)
        this.$refs.robotSearch.setKmSearchContent(search)
        // if (search) {
        //   bus.$emit('fastSearchAI', { data: search })
        // }
      }
    },
    sendKmContent (type, contentTxt, content) {
      let data = {}
      if (contentTxt) {
        data = {
          content: contentTxt,
          type: type
        }
      } else {
        data = {
          content: content,
          type: type
        }
      }
      bus.$emit('kmContentChange', data)
    },
    // 获取机器人配置
    getRobotConfig () {
      let user = this.$store.state.session.user
      const data = {
        account: user ? user.account : ''
      }
      this.loading = true
      this.$store.dispatch('queryRobotEnter', data).then(res => {
        if (res.message.includes('haveRobot')) {
          this.isHaveRobot = true
          this.activeName = 'AIRobot'
        } else {
          this.activeName = 'km'
          this.isHaveRobot = false
        }
        this.loading = false
      })
    }
  },
  components: {
    kmSearch,
    robotSearch
  },
  mounted () {
    if (!(this.webchatData && this.webchatData.robotAssistId)) {
      this.activeName = 'km'
      this.$refs.kmSearch.searchInnerKmList()
    }
    // bus.$on('fastSearchWebchatContent', (data) => {
    //   if (this.webchatData && this.webchatData.robotAssistId) {
    //     this.activeName = 'AIRobot'
    //     bus.$emit('fastSearchAI', data)
    //   } else {
    //     this.activeName = 'km'
    //     bus.$emit('fastSearchKm', data)
    //   }
    // })
    bus.$on('openWebchatKm', () => {
      this.activeName = 'km'
      this.$refs.kmSearch.searchInnerKmList()
    })
    this.getRobotConfig();
  },
  beforeDestroy () {
    bus.$off('fastSearchWebchatContent')
    bus.$off('openWebchatKm')
  }
}
</script>
<style lang="stylus">
.auxil_agent_box
  max-height 500px
  overflow-y scroll
</style>
